package com.goodboys.controller;

import com.goodboys.bean.BaseParamBean;
import com.goodboys.service.IDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by CJ on 2018-03-28.
 */

@RestController
@RequestMapping("/article")
public class IDataController {

    @Autowired
    private IDataService iDataService;

    //获取查询结果列表
    @GetMapping("/list")
    @ResponseBody
    public Map getArticleList(BaseParamBean param){
        Map<String, Object> map = new HashMap<>();
        if(param == null){
            map.put("message","请求参数错误，请重试。。。");
            map.put("status", 0);
            return  map;
        }else {
           return  iDataService.getArticleList(param);
        }
    };

    //获取文章详情
    @ResponseBody
    @GetMapping("detail")
    public Map getArticleDetail(BaseParamBean param){
        Map<String, Object> map = new HashMap<>();
        if(param != null && param.getFilename() != null && param.getDbcode() != null){
            return  iDataService.getArticleDetail(param);
        }else {
            map.put("message","请求参数错误，请重试。。。");
            map.put("status", 0);
            return  map;
        }
    };

    //获取下载链接
    //获取预览链接
    @GetMapping("/download")
    public void   download(@RequestParam("targetk") String targetk, HttpServletResponse response) throws IOException {
        //获取预览链接
        String previewUrl = iDataService.getDownloadUrl(targetk);
        response.sendRedirect(previewUrl);
    }


    //获取预览链接
    @GetMapping("/preview")
    public void   preview(@RequestParam("targetk") String targetk, HttpServletResponse response) throws IOException {
        //获取预览链接
            String previewUrl = iDataService.getPreviewUrl(targetk);
            response.sendRedirect(previewUrl);
    }


    //获取热词的列表
    @GetMapping("/hotWordArticles")
    @ResponseBody
    public Object hotWordsList(@RequestParam("hotword") String hotword){
        Map map = new HashMap();
        if(hotword !=null && !"".equals(hotword) ){
            Object obj= iDataService.getArticleListByHotWord(hotword);
            return  obj;
        }else {
            map.put("message", "参数有误，请检查后重新访问。。，");
            map.put("status", 0);
            return map;
        }
    }

    //获取所有的热词列表
    @GetMapping("/hotWords")
    @ResponseBody
    public Map getHotWordsList(){
        return iDataService.getHotWordsList();
    }


}
